class SearchClause:
    """ An abstract base class that represents a search clause """
    
    def __init__(self, idx, **kwargs):
        """ Initialize the clause """
        
        self.idx    = idx    # The index to query on
        self.kwargs = kwargs # Accept arbitrary parameters
        
    def getArg(self, key, default=None):
        """ Returns the argument with the specified key """        
        
        return self.kwargs.get( key, default )
    
    def toSQL(self, value):
        """ Creates an SQL Statement that represents a search for the specified value """
        
        raise "NotImplemented"
    
    def toKERL(self, value):
        """ Creates a KERL Statement that represents a search for the specified value """
        
        raise "NotImplemented"
    
    
class SearchIndex(SearchClause):
    """ Generates SQL to query a KnowledgeEngine search index """
    
    def toSQL( self, value ):
        """ Creates an SQL Statement that represents a search for the specified value 
            Returns: WhereSQL OR Two-Tuple - (JoinSQL, WhereSQL) 
              
            
            where JoinSQL is to be inserted in the sql JOIN section
            and WhereSQL is to be inserted in the sql WHERE section
        """
       
        searchType = self.getArg( 'searchType' )
        
        # If it's 
        if type( value ) in (list, tuple):
            return self.toMultiSQL( value )
        else:
            return self.toSingleSQL( value )
        
        
    def toMultiSQL( self, value ):
        """ Creates an SQL Statement for a multiple selection """
        
        qstring = self.knowledgeengine_assessmentstorageservice.qstring
        
        queryValue = 'IN (%s)' % ','.join( [qstring(item) for item in value] )
        
        return "AND %s %s" % ( self.idx, queryValue )
        
        
        
        
        